<template>
  <el-dialog :title="!dataForm.id ? '新建' :'编辑'" :close-on-click-modal="false" append-to-body :visible.sync="visible"
    class="JNPF-dialog JNPF-dialog_center" lock-scroll width="600px">
    <el-row :gutter="15" class="">
      <el-form ref="elForm" :model="dataForm" :rules="rules" size="small" label-width="100px" label-position="right">
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label-width="80" label="支付类型" prop="type">
              <el-select v-model="dataForm.type" placeholder="请选择" clearable :style='{"width":"100%"}'>
                <el-option v-for="(item, index) in typeOptions" :key="index" :label="item.fullName" :value="item.enCode"
                  :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label-width="0">
              <div class="JNPF-common-title">
                <h2>支付配置</h2>
              </div>
              <el-table :data="dataForm.assoPayConfigDetailList" size='mini'>
                <el-table-column type="index" width="50" label="序号" align="center" />
                <el-table-column prop="configKey" label="配置KEY">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.configKey" placeholder="请输入" clearable :style='{"width":"100%"}'>
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="configValue" label="配置VALUE">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.configValue" placeholder="请输入" clearable :style='{"width":"100%"}'>
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="50">
                  <template slot-scope="scope">
                    <el-button size="mini" type="text" class="JNPF-table-delBtn"
                      @click="delassoPayConfigDetailList(scope.$index)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <div class="table-actions" @click="addassoPayConfigDetailList()">
                <el-button type="text" icon="el-icon-plus">添加</el-button>
              </div>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
      <SelectDialog v-if="selectDialogVisible" :config="currTableConf" :formData="dataForm" ref="selectDialog"
        @select="addForSelect" @close="selectDialogVisible=false" />
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"> 确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
  import request from '@/utils/request'
  import { getDataInterfaceRes } from '@/api/systemData/dataInterface'
  import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
  export default {
    components: {},
    props: [],
    data() {
      return {
        visible: false,
        loading: false,
        btnLoading: false,
        selectDialogVisible: false,
        currTableConf: {},
        addTableConf: {
          assoPayConfigDetailList: {
            "popupType": "dialog",
            "hasPage": true,
            "popupTitle": "选择数据",
            "pageSize": 20,
            "columnOptions": [],
            "interfaceId": "",
            "interfaceName": "",
            "relationOptions": [],
            "templateJson": [],
            "popupWidth": "800px"
          },
        },
        tableRows: {
          assoPayConfigDetailList: {
            configKey: '',
            configValue: '',
            enabledmark: undefined
          },
        },
        currVmodel: "",
        dataForm: {
          creatoruserid: "",
          creatortime: "",
          type: "",
          assoPayConfigDetailList: [],
        },
        rules: {
          type: [{
            required: true,
            message: '请选择',
            trigger: 'change'
          }, ],
        },
        typeOptions: [],
      }
    },
    computed: {},
    watch: {},
    created() {
      this.gettypeOptions();
    },
    mounted() {},
    methods: {
      t_asso_pay_config_detailExist() {
        let isOk = true;
        for (let i = 0; i < this.dataForm.assoPayConfigDetailList.length; i++) {
          const e = this.dataForm.assoPayConfigDetailList[i];
        }
        return isOk;
      },
      gettypeOptions() {
        getDictionaryDataSelector('367299187424887877').then(res => {
          this.typeOptions = res.data.list
        })
      },
      clearData() {
        this.dataForm.creatoruserid = "";
        this.dataForm.creatortime = "";
        this.dataForm.type = "";
        this.dataForm.assoPayConfigDetailList = [];
      },
      init(id) {
        this.dataForm.id = id || 0;
        this.visible = true;
        this.$nextTick(() => {
          this.$refs['elForm'].resetFields();
          if (this.dataForm.id) {
            this.loading = true
            request({
              url: '/api/asso/AssoPayConfig/' + this.dataForm.id,
              method: 'get'
            }).then(res => {
              this.dataInfo(res.data)
              this.loading = false
            });
          } else {
            this.clearData()
          }
        });
        this.$store.commit('generator/UPDATE_RELATION_DATA', {})
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['elForm'].validate((valid) => {
          if (valid) {
            if (!this.t_asso_pay_config_detailExist()) return
            this.request()
          }
        })
      },
      request() {
        this.btnLoading = true
        let _data = this.dataList()
        if (!this.dataForm.id) {
          request({
            url: '/api/asso/AssoPayConfig',
            method: 'post',
            data: _data
          }).then((res) => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.visible = false
                this.btnLoading = false
                this.$emit('refresh', true)
              }
            })
          }).catch(() => {
            this.btnLoading = false
          })
        } else {
          request({
            url: '/api/asso/AssoPayConfig/' + this.dataForm.id,
            method: 'PUT',
            data: _data
          }).then((res) => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.visible = false
                this.btnLoading = false
                this.$emit('refresh', true)
              }
            })
          }).catch(() => {
            this.btnLoading = false
          })
        }
      },
      addassoPayConfigDetailList() {
        let item = {
          configKey: undefined,
          configValue: undefined,
        }
        this.dataForm.assoPayConfigDetailList.push(item)
      },
      delassoPayConfigDetailList(index) {
        this.dataForm.assoPayConfigDetailList.splice(index, 1);
      },
      openSelectDialog(key) {
        this.currTableConf = this.addTableConf[key]
        this.currVmodel = key
        this.selectDialogVisible = true
        this.$nextTick(() => {
          this.$refs.selectDialog.init()
        })
      },
      addForSelect(data) {
        for (let i = 0; i < data.length; i++) {
          let item = {
            ...this.tableRows[this.currVmodel],
            ...data[i]
          }
          this.dataForm[this.currVmodel].push(item)
        }
      },
      dataList() {
        var _data = JSON.parse(JSON.stringify(this.dataForm));
        for (let i = 0; i < _data.assoPayConfigDetailList.length; i++) {
          var _list = _data.assoPayConfigDetailList[i];
        }
        return _data;
      },
      dataInfo(dataAll) {
        let _dataAll = dataAll
        for (let i = 0; i < _dataAll.assoPayConfigDetailList.length; i++) {
          var _list = _dataAll.assoPayConfigDetailList[i];
        }
        this.dataForm = _dataAll
      },
    },
  }
</script>
